package com.yang.chen.oa.controller;

import com.yang.chen.oa.domain.TbDept;
import com.yang.chen.oa.domain.TbEmployee;
import com.yang.chen.oa.domain.TbJob;
import com.yang.chen.oa.service.DeptService;
import com.yang.chen.oa.service.EmployeeService;
import com.yang.chen.oa.service.JobService;
import com.yang.chen.oa.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DeptService deptservice;

    @Autowired
    private JobService jobService;


    @RequestMapping("/employee/selectEmployee")
    public String selectEmployee(HttpSession session) {

        List<TbDept> deptList = employeeService.selectDept();
        List<TbJob> jobList = employeeService.selectJob();
        session.setAttribute("deptList", deptList);
        session.setAttribute("jobList", jobList);

        return "employee/employee";
    }

    @RequestMapping("/ejson")
    public @ResponseBody
    PageBean<TbEmployee> showAllEmployee(Integer pageNumber, Integer pageSize, TbEmployee employee) {
        PageBean<TbEmployee> pageBean = employeeService.showEmployee(pageNumber, pageSize, employee);
        return pageBean;

    }

    @RequestMapping("/employee/updateEmployee")
    public String updateEmployee(Integer flag, Integer id, Model model, @RequestParam(required = false, value = "") TbEmployee employee, HttpServletResponse response, HttpServletRequest request) {
        System.out.println("udpate");
        if (flag == 1) {
            TbEmployee employee1 = employeeService.findEmployeeById(id);

            List<TbDept> tbDepts = deptservice.selectAllDept();
            List<TbJob> tbJobs = jobService.selectAllJob();

            request.setAttribute("deptList", tbDepts);
            request.setAttribute("jobList", tbJobs);
            model.addAttribute("employee", employee1);
            return "employee/showUpdateEmployee";
        } else {

            try {
                System.out.println(employee);
                response.getWriter().print("success");
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }

    }

    @RequestMapping("/employee/addEmployee")
    public String addEmployee(Integer flag, TbEmployee employee, HttpServletResponse response, HttpServletRequest request) {
        if (flag == 1) {

            List<TbDept> tbDepts = deptservice.selectAllDept();
            List<TbJob> tbJobs = jobService.selectAllJob();

            request.setAttribute("deptList", tbDepts);
            request.setAttribute("jobList", tbJobs);

            return "employee/showAddEmployee";
        } else {
            if (employeeService.addEmployee(employee)) {
                try {
                    response.getWriter().print("success");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    response.getWriter().print("error");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return null;
    }

    @RequestMapping("/employee/removeEmployee")
    public String deleteDept(Integer flag, Integer id, @RequestParam(required = false, value = "ids[]") Integer[] ids,
                             HttpServletResponse response) {
        PrintWriter writer;
        try {
            writer = response.getWriter();

            boolean result = false;
            if (flag == 1) {
                result = employeeService.deleteById(id);
                return "employee/employee";

            } else {
                result = employeeService.deleteBybatch(ids);

            }
            if (result) {
                writer.print("success");
            } else {
                writer.print("error");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

}
